package com.application.lucky.presenter;

import com.application.lucky.bean.jsonBean.LoginBean;
import com.application.lucky.manager.ContactsManager;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.LoginRequest;
import com.application.lucky.view.LoginView;
import com.application.mylibrary.base.mvp.IBasePresenter;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.baselib.libnetwork.net.libnetworkcomponent.RequestCommonParams;

public class LoginPresenter extends IBasePresenter <LoginView>{

    private final LoginRequest request;

    public LoginPresenter() {
        request = new LoginRequest();

    }

    public void login(){
        if (getView()!=null) {
            final String username = getView().getUsername();
            String password = getView().getPassword();
            request.login(username, password, new LoginRequest.LoginBack<BaseResponse<LoginBean>>() {


                @Override
                public void success(BaseResponse<LoginBean> s) {
                    LoginBean dataInfo = s.dataInfo;

                    if (dataInfo != null) {
                        String token = dataInfo.token;
                        UserManagers.getInstance().saveLoginName(username);
                        RequestCommonParams.getInstance().updateToken(token);
                        UserManagers.getInstance().saveToken(token);
                        loadUser();
                    }

                }

                @Override
                public void fail(int code) {
                    if (getView()!=null) {
                        getView().error(code);
                    }
                }
            });


        }
    }

    public void loadUser() {
        UserManagers.getInstance().loadUserBean();
        ContactsManager.getInstance().loadFriends();

        if (getView()!=null) {
            getView().Success();
        }
    }


    public void setPassword(){
        if (getView()!=null) {
            String username = getView().getUsername();
            String password = getView().getPassword();


        }
    }
}
